package com.onccc.social.step

import android.app.ActivityManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.accessibility.AccessibilityNodeInfo
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.Utils
import com.onccc.social.Assists
import com.onccc.social.Assists.click
import com.onccc.social.Assists.findFirstParentClickable
import com.onccc.social.Assists.getBoundsInScreen
import com.onccc.social.Assists.log
import com.onccc.social.Assists.paste
import com.onccc.social.AssistsWindowManager
import com.onccc.social.MainActivity
import com.onccc.social.OverManager
import com.onccc.social.stepper.Step
import com.onccc.social.stepper.StepCollector
import com.onccc.social.stepper.StepImpl
import kotlinx.coroutines.delay

/**
 * @author GongChengHao
 * @date 2024/11/29
 * Description :
 */
class WXVideoRaising : StepImpl() {

    private val TAG = "com.onccc.social.demo"

    private var maxSelect = 5
    private var selectIndex = 0
    override fun onImpl(collector: StepCollector) {
        collector.next(StepTag.STEP_1) { it ->
            OverManager.log("启动微信")
            Intent().apply {
                addCategory(Intent.CATEGORY_LAUNCHER)
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                component = ComponentName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI")
                Assists.service?.startActivity(this)
            }
            it.data?.let {
//                maxSelect = it as Int
            }
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_2) {

            Assists.findByText("发现").forEach {
                val screen = it.getBoundsInScreen()
                if (screen.left > Assists.getX(1080, 630) &&
                        screen.top > Assists.getX(1920, 1850)
                ) {
                    OverManager.log("已打开微信主页，点击【发现】")
                    it.parent.parent.click()
                    return@next Step.get(StepTag.STEP_3)
                }
            }

            if (it.repeatCount == 5) {
                return@next Step.get(StepTag.STEP_1)
            }

            return@next Step.repeat
        }.next(StepTag.STEP_3) {
            runIO { delay(1522) }
            Assists.findByText("视频号").forEach {
                selectIndex = 0
                val screen = it.getBoundsInScreen()
                Log.e("$TAG=====:", "left : " + screen.left + " top : " + screen.top + " right : " + screen.right + " bottom : " + screen.bottom)
                if (screen.left > 120 && screen.top > 400) {
                    if (it.isClickable) {
                        it.click()
                        runIO { delay(1372) }
                    } else {
                        it.findFirstParentClickable()?.let { pat ->
                            pat.log()
                            pat.click()
                            runIO { delay(1372) }
                        }
                    }
                    return@next Step.get(StepTag.STEP_4)
                }
            }
            return@next Step.none

        }.next(StepTag.STEP_4) {
            runIO { delay(2072) }
            Assists.findByText("搜索").firstOrNull() {
                OverManager.log("点击搜索")
                val screen = it.getBoundsInScreen()
                Log.e("==========:", "left : " + screen.left + " top : " + screen.top)
                it.findFirstParentClickable()?.let {
                    it.log()
                    it.click()
                }
                return@next Step.get(StepTag.STEP_5)
            }
            if (it.repeatCount == 5) {
                OverManager.log("未进入找到搜索页")
                val s = Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
                s.killBackgroundProcesses("com.tencent.mm")
                runIO {
                    delay(2000)
                }
                return@next Step.get(StepTag.STEP_1)
            }
            return@next Step.repeat
        }.next(StepTag.STEP_5) {
            runIO { delay(1000) }
            OverManager.log("输入内容")
            Assists.findByTags("android.widget.EditText").forEach {
                it.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
                runIO { delay(MainActivity.getOneTime()) }
                for (s in MainActivity.getKeyword()){
                    it.paste(s.toString())
                    runIO { delay(MainActivity.getInputTime()) }
                }
                return@next Step.get(StepTag.STEP_6)
            }

            return@next Step.none
        }.next(StepTag.STEP_6) {
            runIO { delay(1000) }
            OverManager.log("点击搜索")
            Assists.findById("com.tencent.mm:id/mdx").forEach {
                OverManager.log("搜索输入框内容")
                it.click()
                return@next Step.get(StepTag.STEP_7)
            }

            return@next Step.none
        }.next(StepTag.STEP_7) {
            runIO { delay(2000) }
            OverManager.log("向下滑动")
            AssistsWindowManager.switchNotTouchableAll()
            runIO { delay(250) }
            val x = ScreenUtils.getAppScreenWidth() / 2F
            val distance = ScreenUtils.getAppScreenHeight() / 2F
            val startY = distance + distance / 2F
            val endY = distance - distance / 2F
            OverManager.log("滑动：$x/$startY,$x/$endY")
            Assists.gesture(
                    floatArrayOf(x, startY), floatArrayOf(x, endY), 0, 2000L
            )
            AssistsWindowManager.switchTouchableAll()
            runIO {
                OverManager.log("开始点击")
                delay(1000)
            }
            return@next Step.get(StepTag.STEP_8)
        }.next(StepTag.STEP_8) {
            runIO {
                delay(2000)
            }
            val x = ScreenUtils.getAppScreenWidth() / 4F
            val y = ScreenUtils.getAppScreenHeight() / 3F
            val clickTime = 10L
            OverManager.log("点击：$x,$y")
            Assists.gestureClick(x, y, clickTime)
            runIO { delay(1000) }
            if (selectIndex == maxSelect) {
                return@next Step.get(StepTag.STEP_10)
            }

            runIO { delay(2000) }
            OverManager.log("点赞")
            Assists.findById("com.tencent.mm:id/a_x").forEach {
                it.click()
//                selectIndex++

//                Assists.findByText("返回").forEach {
//                    it.click()
//                    runIO { delay(2000) }
//                    return@next Step.get(StepTag.STEP_7)
//                }
                return@next Step.get(StepTag.STEP_9)
            }
            return@next Step.none

        }.next(StepTag.STEP_9) {
            runIO { delay(1000) }
            OverManager.log("点击评论按钮")
            Assists.findById("com.tencent.mm:id/c79").forEach {
                it.click()
                runIO { delay(2000) }
                return@next Step.get(StepTag.STEP_10)
            }
//            OverManager.log("点击发表")
//            Assists.findByText("发表").forEach {
//                it.log("发表")
//                it.click()
//                runIO { delay(15000) }
//                return@next Step.get(StepTag.STEP_10)
//            }
            return@next Step.none
        }.next(StepTag.STEP_10) {
            runIO { delay(1000) }
            OverManager.log("输入评论内容")
            Assists.findByTags("android.widget.EditText").forEach {
                it.click()
                it.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
                it.paste("不错差不多")

                runIO { delay(1000) }
                OverManager.log("点击发送")
                Assists.findByText("发送").forEach {
                    OverManager.log("搜索输入框内容")
                    it.click()
                    return@next Step.get(StepTag.STEP_11)
                }
            }
            return@next Step.none
        }.next(StepTag.STEP_11) {
            val x = ScreenUtils.getAppScreenWidth() / 4F
            val y = ScreenUtils.getAppScreenHeight() / 5F
            val clickTime = 10L
            Assists.gestureClick(x, y, clickTime)
            runIO { delay(1000) }

            OverManager.log("返回")
            Assists.findByText("返回").forEach {
                val screen = it.getBoundsInScreen()
                Log.e("==========:", "left : " + screen.left + " top : " + screen.top)
                it.findFirstParentClickable()?.let {
                    it.log()
                    it.click()
                    runIO { delay(1000) }
                }
                return@next Step.get(StepTag.STEP_100)
            }


            return@next Step.none
        }.next(StepTag.STEP_100) {

            val s = Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            s.killBackgroundProcesses("com.tencent.mm")
            runIO {
                delay(2000)
            }
            return@next Step.none
        }
    }

    fun getRandomTitle() {


    }

}